package ws

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"github.com/pkg/errors"
	"net/http"
)

var (
	websocketUpgrade = websocket.Upgrader{
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}
)

func ServeWS(c *gin.Context) (*websocket.Conn, error) {
	wsConn, err := websocketUpgrade.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		return nil, errors.WithStack(fmt.Errorf("websocketUpgrade.Upgrade err:%v", err))
	}
	return wsConn, nil
}
